package com.taxt.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.taxt.R;
import com.taxt.aop.Log;
import com.taxt.aop.SingleClick;
import com.taxt.app.AppActivity;
import com.taxt.http.api.DeclareDataApi;
import com.taxt.http.model.HttpData;
import com.taxt.other.SpUtils;
import com.taxt.ui.adapter.DeclareDataAdapter;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;


/**
 *    author : wq

 *    time   : 2019/04/20
 *    desc   : 收入纳税明细
 */
public final class PersonalDeclareDetailDataActivity extends AppActivity {

    private List<DeclareDataApi.Bean>list=new ArrayList<>();
    private static final String INTENT_KEY_IN_YEAR = "bean";
    private DeclareDataApi.Bean.SalaryListDTO bean;
    private SettingBar sbTotalMoney;
    private SettingBar sbDeclareTotalMoney;
    private SettingBar declareIncome;
    private TextView declareTaxMoney;
    private SettingBar declareIncomeType;
    private TextView sbPersonName;
    private TextView sbPersonNum;
    private TextView sbPersonCompetent;
    private TextView sbCompetent;
    private SettingBar declareChannel;
    private SettingBar declareDate;
    private SettingBar declareMoneyDate;
    private SettingBar declareOwnIncome;
    private SettingBar declareOwnFreeIncome;
    private SettingBar declareSaleMoney;
    private SettingBar declareOwnSaleMoney;
    private SettingBar declareOwn;
    private SettingBar declareCurrent;
    private LinearLayout currentLayout;
    private SettingBar declareCurrentDeduction;
    private SettingBar declareCurrentDeduction1;
    private SettingBar declareCurrentDeduction2;
    private SettingBar declareCurrentDeduction3;
    boolean isSelect=false;
    private TextView tipsText;

    @Log
    public static void start(Context context, DeclareDataApi.Bean.SalaryListDTO bean) {
        Intent intent = new Intent(context, PersonalDeclareDetailDataActivity.class);
        intent.putExtra(INTENT_KEY_IN_YEAR, bean);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.personal_declare_detail_activity;
    }

    @Override
    protected void initView() {
        sbTotalMoney = (SettingBar) findViewById(R.id.sb_total_money);
        sbDeclareTotalMoney = (SettingBar) findViewById(R.id.sb_declare_total_money);
        declareIncome = (SettingBar) findViewById(R.id.declare_income);
        declareTaxMoney =  findViewById(R.id.declare_tax_money);
        declareIncomeType = (SettingBar) findViewById(R.id.declare_income_type);
        sbPersonName = (TextView) findViewById(R.id.sb_person_name);
        sbPersonNum = (TextView) findViewById(R.id.sb_person_num);
        sbPersonCompetent = (TextView) findViewById(R.id.sb_person_competent);
        sbCompetent = (TextView) findViewById(R.id.sb_competent);
        declareChannel = (SettingBar) findViewById(R.id.declare_channel);
        declareDate = (SettingBar) findViewById(R.id.declare_date);
        declareMoneyDate = (SettingBar) findViewById(R.id.declare_money_date);
        declareOwnIncome = (SettingBar) findViewById(R.id.declare_own_income);
        declareOwnFreeIncome = (SettingBar) findViewById(R.id.declare_own_free_income);
        declareSaleMoney = (SettingBar) findViewById(R.id.declare_sale_money);
        declareOwnSaleMoney = (SettingBar) findViewById(R.id.declare_own_sale_money);
        declareOwn = (SettingBar) findViewById(R.id.declare_own);
        declareCurrent = (SettingBar) findViewById(R.id.declare_current);
        currentLayout = (LinearLayout) findViewById(R.id.current_layout);
        declareCurrentDeduction = (SettingBar) findViewById(R.id.declare_current_deduction);
        declareCurrentDeduction1 = (SettingBar) findViewById(R.id.declare_current_deduction1);
        declareCurrentDeduction2 = (SettingBar) findViewById(R.id.declare_current_deduction2);
        declareCurrentDeduction3 = (SettingBar) findViewById(R.id.declare_current_deduction3);
        setOnClickListener(declareCurrent);
        tipsText = (TextView) findViewById(R.id.tips_text);
    }

    @Override
    protected void initData() {
        bean=(DeclareDataApi.Bean.SalaryListDTO)getIntent().getSerializableExtra(INTENT_KEY_IN_YEAR);
        String beforeText=getString(R.string.declare_tax_tips);
        SpannableStringBuilder builder = new SpannableStringBuilder(beforeText); //创建SpannableStringBuilder，并添加前面文案
        builder.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, beforeText.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置前面的字体颜色
        builder.setSpan(new AbsoluteSizeSpan(15, true), 0, beforeText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //设置前面的字体大小
        builder.append("查看税款计算"); //追加后面文案
        builder.setSpan(new ForegroundColorSpan(Color.parseColor("#2D67B3")),beforeText.length(), builder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置后面的字体颜色
        builder.setSpan(new AbsoluteSizeSpan(14, true), beforeText.length(), builder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//设置后面的字体大小
        tipsText.setText(builder);
        getJobData();
    }



    public void getJobData(){
       if(bean!=null){
           declareIncome.setRightText(bean.getIncome()+"元");
           declareTaxMoney.setText(bean.getDeclaredTaxAmount()+"元");
           declareIncomeType.setRightText(bean.getIncomeCategoryItem());
           sbPersonName.setText(bean.getWithholdingAgentName());
           sbPersonNum.setText(bean.getWithholdingAgentId());
           if(bean.getCompetentTaxAuthority().length()>10){
               sbCompetent.setVisibility(View.VISIBLE);
               String str=bean.getCompetentTaxAuthority().substring(bean.getCompetentTaxAuthority().length() - 2);
               sbPersonCompetent.setText(bean.getCompetentTaxAuthority().substring(0,(bean.getCompetentTaxAuthority().length() - 2)));
               sbCompetent.setText(str);
           }else {
               sbCompetent.setVisibility(View.GONE);
               sbPersonCompetent.setText(bean.getCompetentTaxAuthority());
           }
           declareChannel.setRightText(bean.getReportingChannels());
           declareDate.setRightText(bean.getDeclarationDate());
           declareMoneyDate.setRightText(bean.getTaxPeriod());
           declareOwnIncome.setRightText(bean.getCurrentIncome()+"元");
           declareSaleMoney.setRightText(bean.getCurrentPeriodDeducted()+"元");
           declareOwnFreeIncome.setRightText(bean.getCurrentIncomeTaxExempt()+"元");
           declareOwnSaleMoney.setRightText(bean.getCurrentPeriodOtherDeduction1()+"元");
           declareOwn.setRightText(bean.getCurrentPeriodOtherDeduction1()+"元");
           double currentMoney=Double.parseDouble(bean.getCurrentPeriodSpecialDeduction1())+
                   Double.parseDouble(bean.getCurrentPeriodSpecialDeduction2())+
                   Double.parseDouble(bean.getCurrentPeriodSpecialDeduction3())+
                   Double.parseDouble(bean.getCurrentPeriodSpecialDeduction4());
           BigDecimal bg = new BigDecimal(currentMoney);
           double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
           declareCurrent.setRightText(f1+"元");
           declareCurrentDeduction.setRightText(bean.getCurrentPeriodSpecialDeduction1()+"元");
           declareCurrentDeduction1.setRightText(bean.getCurrentPeriodSpecialDeduction2()+"元");
           declareCurrentDeduction2.setRightText(bean.getCurrentPeriodSpecialDeduction3()+"元");
           declareCurrentDeduction3.setRightText(bean.getCurrentPeriodSpecialDeduction4()+"元");
       }
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        if (view == declareCurrent) {
              if(isSelect){
                  currentLayout.setVisibility(View.GONE);
                  isSelect=false;
                  declareCurrent.setRightDrawable(R.drawable.arrows_bottom_ic);
              }else{
                  isSelect=true;
                  currentLayout.setVisibility(View.VISIBLE);
                  declareCurrent.setRightDrawable(R.drawable.arrows_top_ic);
              }
            return;
        }
    }
}